package achievement.dao.Impl;

import achievement.dao.SzlReportDao;
import achievement.domain.SzlReport;
import cn.hutool.core.util.StrUtil;
import utils.JDBCUtils;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.util.ArrayList;
import java.util.List;

public class SzlReportDaoImpl implements SzlReportDao {
    private Connection con;
    private PreparedStatement psmt;
    private ResultSet rs;
    @Override
    public void insert(SzlReport szlReport) {
        String sql = "insert into szl_report\n" +
                "        (report_name,report_type,report_time,report_rank,report_proof) \n" +
                "values(?,?,?,?,?)";
        try {
            con = JDBCUtils.getConnection();
            psmt = con.prepareStatement(sql);
            psmt.setString(1,szlReport.getReportName());
            psmt.setString(2,szlReport.getReportType());
            psmt.setObject(3,szlReport.getReportTime());
            psmt.setInt(4,szlReport.getReportRank());
            psmt.setString(5,szlReport.getReportProof());
            psmt.executeUpdate();
        }catch (Exception e){
            e.printStackTrace();
        }finally {
            JDBCUtils.closeResource(con,psmt);
        }
    }

    @Override
    public void updateById(SzlReport szlReport) {
        String sql = "update szl_report set";
        List<Object> param = new ArrayList<>();
        if (szlReport == null){
            return;
        }else{
            if(szlReport.getReportId() != null){
                sql += " report_id = ?,";
                param.add(szlReport.getReportId());
            }
            if(szlReport.getReportName() != null){
                sql += " report_name = ?,";
                param.add(szlReport.getReportName());
            }
            if(szlReport.getType() != null){
                sql += " type = ?,";
                param.add(szlReport.getType());
            }
            if(szlReport.getReportType() != null){
                sql += " report_type = ?,";
                param.add(szlReport.getReportType());
            }
            if(szlReport.getReportTime() != null){
                sql += " report_time = ?,";
                param.add(szlReport.getReportTime());
            }
            if(szlReport.getReportRank() != null){
                sql += " report_rank = ?,";
                param.add(szlReport.getReportRank());
            }
            if(szlReport.getReportProof() != null){
                sql += " report_proof = ?,";
                param.add(szlReport.getReportProof());
            }
        }
        sql = StrUtil.strip(sql,null,",");
        sql+=" where report_id = ?";
        try {
            con = JDBCUtils.getConnection();
            psmt = con.prepareStatement(sql);
            for(int i = 0; i < param.size(); i++){
                psmt.setObject(i+1,param.get(i));
            }
            psmt.setLong(param.size()+1,szlReport.getReportId());
            psmt.executeUpdate();
        }catch (Exception e){
            e.printStackTrace();
        }finally {
            JDBCUtils.closeResource(con,psmt);
        }
    }

    @Override
    public void deleteById(Long id) {
        String sql = "delete from szl_report where report_id = ?";
        try {
            con = JDBCUtils.getConnection();
            psmt = con.prepareStatement(sql);
            psmt.setLong(1,id);
            psmt.executeUpdate();
        }catch (Exception e){
            e.printStackTrace();
        }finally {
            JDBCUtils.closeResource(con,psmt);
        }
    }

    @Override
    public SzlReport selectById(Long id) {
        String sql = "select * from szl_report where report_id = ?";
        SzlReport szlReport = new SzlReport();
        try {
            con = JDBCUtils.getConnection();
            psmt = con.prepareStatement(sql);
            psmt.setLong(1,id);
            rs = psmt.executeQuery();
            while (rs.next()){
                szlReport.setReportId(rs.getLong(1));
                szlReport.setReportName(rs.getString(2));
                szlReport.setType(rs.getInt(3));
                szlReport.setReportType(rs.getString(4));
                szlReport.setReportTime(rs.getDate(5));
                szlReport.setReportRank(rs.getInt(6));
                szlReport.setReportProof(rs.getString(7));
            }
        }catch (Exception e){
            e.printStackTrace();
        }finally {
            JDBCUtils.closeResource(con,psmt,rs);
        }
        return szlReport;
    }

    @Override
    public List<SzlReport> selectList(SzlReport szlReport) {
        String sql = "select * from szl_report where 1=1";
        List<SzlReport> list = new ArrayList<>();
        List<Object> param = new ArrayList<>();
        if (szlReport == null){
            sql = "select * from szl_report";
        }else{
            if(szlReport.getReportId() != null){
                sql += " and report_id = ?";
                param.add(szlReport.getReportId());
            }
            if(szlReport.getReportName() != null){
                sql += " and report_name like \"%\"?\"%\"";
                param.add(szlReport.getReportName());
            }
            if(szlReport.getType() != null){
                sql += " and type = ?";
                param.add(szlReport.getType());
            }
            if(szlReport.getReportType() != null){
                sql += " and report_type like \"%\"?\"%\"";
                param.add(szlReport.getReportType());
            }
            if(szlReport.getReportTime() != null){
                sql += " and report_time = ?";
                param.add(szlReport.getReportTime());
            }
            if(szlReport.getReportRank() != null){
                sql += " and report_rank = ?";
                param.add(szlReport.getReportRank());
            }
            if(szlReport.getReportProof() != null){
                sql += " and report_proof like \"%\"?\"%\"";
                param.add(szlReport.getReportProof());
            }
        }
        try {
            con = JDBCUtils.getConnection();
            psmt = con.prepareStatement(sql);
            for(int i = 0; i < param.size(); i++){
                psmt.setObject(i+1,param.get(i));
            }
            rs = psmt.executeQuery();
            while (rs.next()){
                SzlReport szlReport1 = new SzlReport();
                szlReport1.setReportId(rs.getLong(1));
                szlReport1.setReportName(rs.getString(2));
                szlReport1.setType(rs.getInt(3));
                szlReport1.setReportType(rs.getString(4));
                szlReport1.setReportTime(rs.getDate(5));
                szlReport1.setReportRank(rs.getInt(6));
                szlReport1.setReportProof(rs.getString(7));
                list.add(szlReport1);
            }
        }catch (Exception e){
            e.printStackTrace();
        }finally {
            JDBCUtils.closeResource(con,psmt,rs);
        }
        return list ;
    }
}
